﻿#encoding=utf-8

from django.http import HttpResponse
from django.shortcuts import render
from TPM_WEB.models import User
from TPM_WEB.models import Error
#from TPM_WEB.models import Plan
from django.db.models import Q
from datetime import datetime,date,timedelta
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header


def send_mail(request):

    mail_host = "spam.avita.com.tw"  # 设置服务器

    mail_user = "tao_lu"  # 用户名

    mail_pass = "P2ss.w0rd"  # 口令


    sender = 'tao_lu@avita.com.tw'
    receivers_check = ['tao_lu@avita.com.tw']
    receivers_confirm = ['tao_lu@avita.com.tw']

    # 邮件正文内容
    today = date.today()
    today_plan = Plan.objects.filter(date=today)
    success = []
    failure = []
    for item in today_plan:
        htmlTable_check = ''
        htmlTable_confirm = ''
        
        plan_id = item.id
        equipment_name = item.equipment.name
        equipment_sn = item.equipment.sn
        day = item.date.strftime('%Y-%m-%d')

        check_man = item.check_man.name
        comfirm_man = item.comfirm_man.name

        check_man_email = item.check_man.email
        comfirm_man_email = item.comfirm_man.email

        check_man_phone = item.check_man.phone
        comfirm_man_phone = item.comfirm_man.phone

        maintence_day = item.equipment.maintence_day
        maintence_weekend = item.equipment.maintence_weekend
        maintence_month = item.equipment.maintence_month

        htmlTable_check += '''
              <tr>
                <td class="tg-baqh">''' + str(day) + '''</td>
                <td class="tg-baqh">''' + equipment_name + '''</td>
                <td class="tg-baqh">''' + equipment_sn + '''</td>
                <td class="tg-baqh">''' + check_man + '''</td>
                <td class="tg-baqh">''' + comfirm_man + '''</td>
                <td class="tg-baqh">''' + maintence_day + '''</td>
                <td class="tg-baqh">''' + maintence_weekend + '''</td>
                <td class="tg-baqh">''' + maintence_month + '''</td>
                <td class="tg-baqh"><a href="http://tpm.avita.com.tw/confirm?id='''+ str(plan_id) + '&type=0"'+ u'''>点击确认</a></td>
              </tr>'''
        htmlTable_confirm += '''
              <tr>
                <td class="tg-baqh">''' + str(day) + '''</td>
                <td class="tg-baqh">''' + equipment_name + '''</td>
                <td class="tg-baqh">''' + equipment_sn + '''</td>
                <td class="tg-baqh">''' + check_man + '''</td>
                <td class="tg-baqh">''' + comfirm_man + '''</td>
                <td class="tg-baqh">''' + maintence_day + '''</td>
                <td class="tg-baqh">''' + maintence_weekend + '''</td>
                <td class="tg-baqh">''' + maintence_month + '''</td>

                
                <td class="tg-baqh"><a href="http://tpm.avita.com.tw/confirm?id='''+ str(plan_id) + '&type=1"'+u''''>点击确认</a></td>
              </tr>'''

        htmlContent_check = u'''<html>
            <head>
        
                <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
                <meta http-equiv="content-type" content="text/html;charset=utf-8">
        
        
        <title>TPM</title>
        
        <style type="text/css">
        .tg  {border-collapse:collapse;border-spacing:0;border-color:#aabcfe;}
        .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#669;background-color:#e8edff;}
        .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#039;background-color:#b9c9fe;}
        .tg .tg-baqh{text-align:center;vertical-align:top}
        .tg .tg-amwm{font-weight:bold;text-align:center;vertical-align:top}
        </style>
        </head>
        <body>
        <table class="tg">
          <tr>
            <th class="tg-amwm">日期</th>
            <th class="tg-amwm">设备名称</th>
            <th class="tg-amwm">设备编号</th>
            <th class="tg-amwm">保养人</th>
            <th class="tg-amwm">确认人</th>
            <th class="tg-amwm">日保养</th>
            <th class="tg-amwm">周保养</th>
            <th class="tg-amwm">月保养</th>
            <th class="tg-amwm">保养人确认</th>
          </tr>
          ''' + htmlTable_check + u'''</table></body>'''

        htmlContent_confirm = u'''<html>
            <head>
        
                <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
                <meta http-equiv="content-type" content="text/html;charset=utf-8">
        
        
        <title>TPM</title>
        
        <style type="text/css">
        .tg  {border-collapse:collapse;border-spacing:0;border-color:#aabcfe;}
        .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#669;background-color:#e8edff;}
        .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#039;background-color:#b9c9fe;}
        .tg .tg-baqh{text-align:center;vertical-align:top}
        .tg .tg-amwm{font-weight:bold;text-align:center;vertical-align:top}
        </style>
        </head>
        <body>
        <table class="tg">
          <tr>
            <th class="tg-amwm">日期</th>
            <th class="tg-amwm">设备名称</th>
            <th class="tg-amwm">设备编号</th>
            <th class="tg-amwm">保养人</th>
            <th class="tg-amwm">确认人</th>
            <th class="tg-amwm">日保养</th>
            <th class="tg-amwm">周保养</th>
            <th class="tg-amwm">月保养</th>
            <th class="tg-amwm">确认人确认</th>
          </tr>
          ''' + htmlTable_confirm + u'''</table></body>'''

        receivers_check.append(check_man_email)
        receivers_confirm.append(comfirm_man_email)

        message = MIMEMultipart()
        message['From'] = Header("AutoMail")
        message['To'] =  Header("CheckMan")
        subject = 'TPM保养计划'
        message['Subject'] = Header(subject, 'utf-8')
        message.attach(MIMEText(htmlContent_check, 'html', 'utf-8'))

        message_confirm = MIMEMultipart()
        message_confirm['From'] = Header("AutoMail")
        message_confirm['To'] =  Header("CheckMan")
        subject = 'TPM保养计划'
        message_confirm['Subject'] = Header(subject, 'utf-8')
        message_confirm.attach(MIMEText(htmlContent_confirm, 'html', 'utf-8'))


        # 构造附件1，传送当前目录下的 test.txt 文件

        #att1 = MIMEText(open('meal.xls', 'rb').read(), 'base64', 'utf-8')

        #att1["Content-Type"] = 'application/octet-stream'

        # 这里的filename可以任意写，写什么名字，邮件中显示什么名字

        #att1["Content-Disposition"] = 'attachment; filename="餐厅刷卡记录.xls"'

        #message.attach(att1)

        try:
            smtpObj = smtplib.SMTP()
            smtpObj.connect(mail_host, 25)  # 25 为 SMTP 端口号

            smtpObj.login(mail_user, mail_pass)
            smtpObj.sendmail(sender, receivers_check, message.as_string())
            smtpObj.sendmail(sender, receivers_confirm, message_confirm.as_string())
            print "邮件发送成功"
            success.append()
        except smtplib.SMTPException:
            failure.append()
        except Exception:
            pass

    return HttpResponse('<H1>发送成功</H1>', content_type='text/html')
